<template>
   <el-dialog   :visible.sync="visible" :title="title"  width="750px"  :before-close="handleClose"  @opened="handleOpen">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">


    <el-form-item label="设备型号" prop="devicemodel">
        <el-input
          v-model="queryParams.devicemodel"
          placeholder="设备型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!-- <el-form-item label="医院" prop="departid">
        <el-select v-model="queryParams.departid" filterable placeholder="请选择医院" @change="chg()">
            <el-option
              v-for="item in deptoptions"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId">
            </el-option>
          </el-select>
      </el-form-item>

      <el-form-item label="使用科室" prop="useoffice">
        <el-select v-model="queryParams.useoffice" placeholder="请选择使用科室" clearable size="small">
          <el-option
            v-for="dict in useofficeOptions"
            :key="dict.deptId"
            :label="dict.deptName"
            :value="dict.deptId"
          />
        </el-select>
      </el-form-item> -->


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleSel"
        >选择</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="医院" align="center" prop="sysDept.deptName" />
      <el-table-column label="设备编号" align="center" prop="devicecode" />
       <el-table-column label="设备型号" align="center" prop="devicemodel" />
      <el-table-column label="设备类别" align="center" prop="devicetype" :formatter="devicetypeFormat" />
      <el-table-column label="使用科室" align="center" prop="useoffice" :formatter="useofficeFormat" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-dialog>
</template>

<script>
import { listnosel, } from "@/api/zyx/device";
import { listAllDept,  } from "@/api/system/dept";
import { addMemberDeviceIds} from "@/api/zyx/memberDevice";

export default {
  name: "selectDeviceList",
  components: {
  },
  data() {
    return {
      memberid:null,
      visible: false,
      title:'提示',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备基础信息表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      deptoptions:[],//医院
      // 设备类别字典字典
      devicetypeOptions: [],
      // 使用科室字典字典
      useofficeOptions: [],
      useofficeOptionsshow:[],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        devicemodel:null,
        devicecode: null,
        memberid:null,
        useoffice:null,
        departid:null,
      },
      addParams: {

        memberid: null,
        ids:null,
      },
    };
  },
  created() {
  this.loading = false;
  listAllDept({deptType:'1'}).then(response => {
    // console.log(response)
    this.deptoptions = response.data;
  });
listAllDept({deptType:'2'}).then(response => {
    // console.log(response)
    this.useofficeOptionsshow = response.data;
  });

  },
  methods: {
    // 使用科室字典字典翻译
    useofficeFormat(row, column) {
      // console.log(this.selectDeptLabel(this.useofficeOptions, row.useoffice))
      return this.selectDeptLabel(this.useofficeOptionsshow, row.useoffice);
    },
    chg(){
      this.queryParams.useoffice=null;
      if(this.queryParams.departid==''){
        this.useofficeOptions =[]
      }else{
        listAllDept({deptType:'2',parentId:this.queryParams.departid}).then(response => {
          // console.log(response)
          this.useofficeOptions = response.data;
        });
      }

    },
    handleOpen(){
      this.getList();
    },
  /** 查询设备基础信息列表 */
      getList() {
        this.loading = true;
        this.queryParams.memberid=this.memberid;
        listnosel(this.queryParams).then(response => {
          this.deviceList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        departid: null,
        devicecode: null,
        devicetype: null,
        useoffice: null,
        deptname:null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeInstalldate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleClose(done) {
      done();
      },
      handleSel(row){
        const ids = row.id || this.ids;
        // console.log('ids',ids)
        this.addParams.memberid=this.memberid
        this.addParams.ids=ids;
        const para=this.addParams;
        this.$confirm('是否确认添加的选中数据项?', "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return addMemberDeviceIds(para);
          }).then(() => {
             this.$parent.handleSrch(this.memberid);
            this.$parent.handleHideAdd();
            // this.getList();
            // this.msgSuccess("删除成功");
          })
      }

  }
};
</script>
